package Offer.offer2020;

/**
 * 替换空格
 */
public class Test04 {
    public static void main(String[] args) {
        String originStr = replaceBlank("we are happy");
        System.out.println(originStr);
    }

    public static String replaceBlank(String originStr) {
        if (originStr == null || originStr.length() == 0) {
            return null;
        }
        int originLength = originStr.length();
        int blankNum = 0;
        char[] ch = originStr.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            if (ch[i] == ' ') {
                blankNum++;
            }
        }

        int newLength = originLength + 2 * blankNum;

        char[] newCh = new char[newLength];

        int index = 0;
        for (int i = 0; i < ch.length; i++) {
            if (ch[i] != ' ') {
                newCh[index++] = ch[i];
            } else {
                newCh[index++] = '%';
                newCh[index++] = '2';
                newCh[index++] = '0';
            }
        }

        return new String(newCh);
    }
}
